layui.config({
    base: "static/layui/module/"
}).extend({
    excel: 'excel/excel'
}).use(['form','layer','jquery','table','laydate','element','laytpl','excel'],function() {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        table = layui.table,
        laydate = layui.laydate,
        element = layui.element,
        laytpl = layui.laytpl,
        excel = layui.excel;

    var upload_type;
    var tableData;
    //加载页面日期控件
    laydate.render({
        elem: '#month'
        ,format: 'yyyy-MM' //可任意组合
        ,type: 'month'
        ,ready: function (date) {
            $(".layui-laydate").on('click', 'ul li', function () {
                $(".layui-laydate").remove();
            });
        }
        ,change: function (value) {
            $("#month").val(value);
        }
        ,done: function (value){
            var shadeIndex = top.layer.msg('数据加载中，请稍候', {icon: 16, time: false, shade: 0.4});
            table.reload('table',{
                url: '/manage/equipment_usagerate/list/'+value
                ,limit: 15
                ,limits: [15,30,45,60,75,90,100]
                ,height: 'full-110'
                ,done: function (res, curr, count){
                    tableData = res.data;
                    top.layer.close(shadeIndex);
                }
            })
        }
    });

    table.on('toolbar(table)', function(obj){
        var tableCheckData = table.checkStatus(obj.config.id);
        tableToolbarEnvent(obj);
        switch(obj.event){
            case 'upload':
                layer.confirm('请选择本次是覆盖导入还是补差导入？PS:覆盖导入将会删除原数据,补差只会导入原数据没有的', {
                        btn: ['覆盖', '补差','取消'] //按钮
                    }
                    ,function () {
                        upload_type = "cover";
                        $("#uploadExcel")[0].click();
                    }
                    ,function () {
                        upload_type = "difference";
                        $("#uploadExcel")[0].click();
                    })
                break;
            case 'templet':
                layer.confirm('确定要下载导入模版到本地吗？', function (index) {
                    window.location.href = "/manage/equipment_usagerate/download_templet";
                    layer.close(index);
                });
                break;
            case 'custom_download':
                if($("#month").val() == ''){
                    layer.msg('请先选择发生月！');
                    return;
                }
                if(tableData.length < 1){
                    layer.msg('当月没有设备使用率！');
                    return;
                }
                layer.confirm('确定要导出数据并下载到本地吗？', function (index) {
                    var conditionArray = new Array();
                    conditionArray.push({
                        field: "happen_month"
                        ,type: "equal"
                        ,value: $("#month").val()
                    });
                    if (top_dc != undefined) {
                        for (let i in top_dc.cacheCondition) {
                            if (top_dc.cacheCondition[i].conditionValueLeftVal.value != ''
                                || top_dc.cacheCondition[i].conditionValueRightVal.value != '' || top_dc.cacheCondition[i].conditionValueVal.value != '') {
                                var condition = {
                                    field: top_dc.cacheCondition[i].conditionFieldVal
                                    , type: top_dc.cacheCondition[i].conditionOptionVal
                                    , right: top_dc.cacheCondition[i].conditionValueRightVal.value
                                    , left: top_dc.cacheCondition[i].conditionValueLeftVal.value
                                    , value: top_dc.cacheCondition[i].conditionValueVal.value
                                }
                                conditionArray.push(condition);
                            }
                        }
                    }
                    window.location.href = "/manage/equipment_usagerate/download?url=cost_equipment_usagerate" + "&condition=" + encodeURI(JSON.stringify(conditionArray));
                    layer.close(index);
                });
                break;
        };
    });
    //拖拽上传
    upload.render({
        elem: '#uploadExcel'
        ,accept: 'file' //普通文件
        ,exts: 'xlsx|xls'
        ,auto: false
        ,choose: function(obj){
            var files = obj.pushFile()
            var fileArr = Object.values(files)// 注意这里的数据需要是数组，所以需要转换一下
            var fields = {
                'equipmentName' : 'A',
                'usageRate' : 'B',
                'happenMonth' : 'C'
            }
            // 用完就清理掉，避免多次选中相同文件时出现问题
            for (var index in files) {
                if (files.hasOwnProperty(index)) {
                    delete files[index]
                }
            }
            $('#uploadExcel').next().val('');
            uploadExcel('/manage/equipment_usagerate/upload/'+upload_type,fields,fileArr,excel,layer,laytpl,element,table)
        }
    });
})